package com.zzl.car.context;

import com.zzl.car.service.IllegalService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ZdpDaemonThread implements DisposableBean,Runnable {

    @Autowired
    private IllegalService illegalService;
    private Thread thread;
    private volatile boolean flag;
    // 间隔时间 单位h毫秒
    private volatile long time = 7*24*60*60*1000;

    public ZdpDaemonThread() {
        flag = true;
        this.thread = new Thread(this);
        this.thread.setDaemon(true);
        this.thread.start();
    }

    // 该线程用于做定时任务
    @Override
    public void run() {
        while(flag){
            // 每隔一段时间，查询违章和计算爱车指数分
            System.out.println("===============守护线程启动....===========");
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 计算违章和爱车指数分
            // 计算违章
            illegalService.getIllegalAndHandler();
        }
    }

    @Override
    public void destroy() throws Exception {
        flag = false;
    }
}
